package com.jy.controller;


import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.search.param.addSearchHistoryParam;
import com.jy.common.tool.JwtUtil;
import com.jy.service.SearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 搜索历史表 前端控制器
 * </p>
 *
 * @author fzg
 * @since 2024-11-12
 */
@RestController
@RequestMapping("/search-history")
public class SearchHistoryController {

    private final SearchHistoryService searchHistoryService;

    public SearchHistoryController(SearchHistoryService searchHistoryService) {
        this.searchHistoryService = searchHistoryService;
    }

    @GetMapping("/getHotSearchKey")
    public ResponseWrapper getHotSearchKey(){
        return searchHistoryService.getHotSearchKey();
    }

    @GetMapping("/deleteHistorySearchKeys")
    public ResponseWrapper deleteHistorySearchKeys(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userAid = JwtUtil.getTokenId(token);
        return searchHistoryService.deleteHistorySearchKeys(userAid);
    }

    @PostMapping("/addSearchHistory")
    public ResponseWrapper addSearchHistory(HttpServletRequest request, @RequestBody addSearchHistoryParam param){
        String token = request.getHeader("token");
        Integer userAid = JwtUtil.getTokenId(token);
        return searchHistoryService.addSearchHistory(userAid,param.getKeyValue());
    }

}
